package com.thread.jucTest;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @program: leetcode
 * @description:
 * @author: wangzhihua
 * @date: 2024-06-23
 */
public class ThreadPoolTest {

	@Test
	public void test1() {
		Executors.newFixedThreadPool(10); // 核心线程数=最大线程数=自定义,线程存活时间0,阻塞队列LinkedBlockingQueue
		Executors.newCachedThreadPool(); // 核心线程数=0,最大线程数Integer.max,线程存活时间60s,阻塞队列SynchronousQueue
		Executors.newSingleThreadExecutor(); // 核心线程数=最大线程数=1,线程存活时间0,阻塞队列LinkedBlockingQueue
		Executors.newScheduledThreadPool(10); // 核心线程数自定义,最大线程数=Integer.max,线程存活时间0,阻塞队列DelayedWorkQueue
		Executors.newWorkStealingPool(); // 底层不是ThreadPoolExecutor,使用所有可用的处理器作为其目标并行级别来创建工作窃取线程池
	}

	@Test
	public void test2() {
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		for (int i = 0; i < 10; i++) {
			int num = i;
			threadPool.execute(() -> {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + " 执行了第" + num + "个任务");
			});
		}
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void test3() {
//		Future<Object> future = new Future<>();
	}

}
